package com.cloudkinto.controller.sowWall;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.sowWall.SowWallAddBo;
import com.cloudkinto.bo.sowWall.SowWallQueryBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.OrderOutboundDao;
import com.cloudkinto.dao.OutboundWaveDao;
import com.cloudkinto.entity.OrderOutboundDo;
import com.cloudkinto.entity.OutboundWaveDo;
import com.cloudkinto.entity.SowWallDo;
import com.cloudkinto.service.sowWall.SowWallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 播种墙 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-12-03
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "播种墙模块")
@RequestMapping("/api/admin/wms/sowWall")
public class SowWallWmsController extends BaseController {
    private final SowWallService sowWallService;
    private final OutboundWaveDao waveDao;
    private final OrderOutboundDao outboundDao;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "播种墙新增", notes = "播种墙新增")
    public SingleResult add(@RequestBody @Validated SowWallAddBo req) {
        return SingleResult.success("新增成功", sowWallService.add(req));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "播种墙根据id删除", notes = "播种墙根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        return SingleResult.success("删除成功", sowWallService.delete(id));
    }


    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "播种墙查询（分页）", notes = "播种墙查询（分页）")
    public PageResult listPage(@RequestBody @Validated SowWallQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return sowWallService.getListPage(bo);
    }

    @UserSessionRequired
    @GetMapping("/getDetailByWave/{waveNo}")
    @ApiOperation(value = "根据波次号查询详情", notes = "根据波次号查询详情")
    public SingleResult getDetailByWave(@PathVariable("waveNo") String waveNo) {
        SowWallDo one = sowWallService.getOne(Wrappers.<SowWallDo>lambdaQuery()
                .eq(SowWallDo::getInUseWaveNo, waveNo).last("limit 1"));
        if (Objects.nonNull(one)) {
            return SingleResult.success("查询成功", Collections.singletonList(one));
        }
        OutboundWaveDo waveDo = waveDao.selectOne(
                Wrappers.<OutboundWaveDo>lambdaQuery()
                        .eq(OutboundWaveDo::getWaveNo, waveNo)
        );
        if (waveDo == null) {
            throw new BizException(SysConstant.WAVE_NOT_EXIST);
        }
        int orderSize = outboundDao.selectCount(Wrappers.<OrderOutboundDo>lambdaQuery().eq(OrderOutboundDo::getWaveId, waveDo.getId()));
        List<SowWallDo> sowWallDoList = sowWallService.lambdaQuery()
                .eq(SowWallDo::getInUse, Boolean.FALSE)
                .apply("column_num * tier_num >" + orderSize)
                .list();
        return SingleResult.success("查询成功", sowWallDoList);
    }

    @UserSessionRequired
    @PutMapping("/setWaveNo")
    @ApiOperation(value = "设置播种墙号", notes = "设置播种墙号")
    public SingleResult setWaveNo(@RequestParam("id") Long id, @RequestParam("waveNo") String waveNo) {
        OutboundWaveDo waveDo = waveDao.selectOne(
                Wrappers.<OutboundWaveDo>lambdaQuery()
                        .eq(OutboundWaveDo::getWaveNo, waveNo)
        );
        if (waveDo == null) {
            throw new BizException(SysConstant.WAVE_NOT_EXIST);
        }
        int orderSize = outboundDao.selectCount(Wrappers.<OrderOutboundDo>lambdaQuery().eq(OrderOutboundDo::getWaveId, waveDo.getId()));
        SowWallDo sowWallDo = sowWallService.getById(id);
        if (sowWallDo.getColumnNum() * sowWallDo.getTierNum() < orderSize) {
            throw new BizException(SysConstant.SowWall_Unable_To_Use);
        }
        sowWallService.lambdaUpdate()
                .eq(SowWallDo::getId, id)
                .set(SowWallDo::getInUse, Boolean.TRUE)
                .set(SowWallDo::getInUseWaveNo, waveNo)
                .update();
        return SingleResult.success("设置成功");
    }
}

